/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.user.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyts.infodb.enums.ModuleNameEnum;
import com.hyts.infodb.enums.SystemConfiguration;
import com.hyts.infodb.toolkit.monitor.HttpMonitor;
import com.hyts.infodb.user.bean.bo.UserBO;
import com.hyts.infodb.user.bean.vo.UserVO;
import com.hyts.infodb.user.bean.dto.LoginParamDTO;
import com.hyts.infodb.user.bean.dto.LoginResultDTO;
import com.hyts.infodb.user.bean.dto.UserDTO;
import com.hyts.infodb.user.service.LoginService;
import com.hyts.infodb.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiBo/Alex
 * @version V1.0
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping(value = "/")   
public class UserDataController {

	@Autowired
	LoginService loginService;

	@Autowired
	UserService service;


	@HttpMonitor
	@ApiOperation(value = "login",notes ="用户登录")
	@PostMapping("/login")
	public LoginResultDTO login(@RequestBody LoginParamDTO dto) {
		LoginResultDTO result = loginService.login(dto);
		return result;
	}

	@ApiOperation(value = "changepwd", notes = "更新密码")
	@PostMapping("/changepwd")
	public LoginResultDTO changePwd(@RequestBody LoginParamDTO dto) {
		LoginResultDTO result = loginService.login(dto);
		return result;
	}

	@ApiOperation(value = "loadUserInfo", notes = "查询基本信息")
	@GetMapping("/load/{id}")
	public UserDTO loadUserInfo(@PathVariable("id") Long id) {
		UserDTO userDTO = new UserDTO();
		UserBO bo = new UserBO();
		bo.setId(id);
		UserVO vo = service.loadUser(bo);
		if(vo == null || vo.getContent() == null) {
			return (UserDTO) userDTO.failure(SystemConfiguration.MessageConfiguration.LOAD, ModuleNameEnum.USER_MODULE.getModuleName());
		}else {
			return (UserDTO) userDTO.success(SystemConfiguration.MessageConfiguration.LOAD,ModuleNameEnum.USER_MODULE.getModuleName(),vo.getContent());
		}
	}

	@ApiOperation(value = "saveUserInfo", notes = "保存基本信息")
	@PostMapping("/saveUserInfo")
	public UserDTO saveUserInfo(@RequestBody UserBO user) {
		UserDTO userDTO = new UserDTO();
		boolean result = service.saveUser(user);
		if(result) {
			return (UserDTO) userDTO.success(SystemConfiguration.MessageConfiguration.SAVE,ModuleNameEnum.USER_MODULE.getModuleName());
		}else {
			return (UserDTO) userDTO.failure(SystemConfiguration.MessageConfiguration.SAVE,ModuleNameEnum.USER_MODULE.getModuleName());
		}
	}

	@ApiOperation(value = "deleteUserInfo", notes = "删除基本信息")
	@DeleteMapping("/deleteUserInfo/{id}")
	public UserDTO deleteUserInfo(@PathVariable("id") Long id) {
		UserDTO userDTO = new UserDTO();
		UserBO bo = new UserBO();
		bo.setId(id);
		boolean result = service.deleteUser(bo);
		if(result) {
			return (UserDTO) userDTO.success(SystemConfiguration.MessageConfiguration.REMOVE,ModuleNameEnum.USER_MODULE.getModuleName());
		}else {
			return (UserDTO) userDTO.failure(SystemConfiguration.MessageConfiguration.REMOVE,ModuleNameEnum.USER_MODULE.getModuleName());
		}
	}

	@ApiOperation(value = "findUserList", notes = "查询用户列表")
	@GetMapping("/find")
	public UserDTO findUserList(@RequestBody UserBO user) {
		UserVO vo = service.findUserList(user);
		UserDTO userDTO = new UserDTO();
		if(vo == null) {
			return (UserDTO) userDTO.failure(SystemConfiguration.MessageConfiguration.QUERY,ModuleNameEnum.USER_MODULE.getModuleName());
		}else {
			return (UserDTO) userDTO.success(SystemConfiguration.MessageConfiguration.QUERY,ModuleNameEnum.USER_MODULE.getModuleName(),vo.getContents());
		}
	}

	@ApiOperation(value = "getUser", notes = "根据用户名查询用户")
	@GetMapping("/get/{userName}")
	public JSONObject getUser(@PathVariable  String userName) {
		UserVO vo = service.getUser(userName);
		return JSONObject.parseObject(JSON.toJSONString(vo));
	}

}
